package com.shinewind.sso.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.shinewind.common.constant.messageConstant;
import com.shinewind.pojo.result.Result;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Component
public class AuthEntryPointHandler implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest req, HttpServletResponse resp, AuthenticationException e) throws IOException, ServletException {
        // 设置响应头
        resp.setContentType("application/json;charset=utf-8");
        // 返回值
        PrintWriter out = resp.getWriter();
        out.write(new ObjectMapper().writeValueAsString(Result.fail(false, messageConstant.AUTH_ERROR)));
        out.flush();
        out.close();
    }
}
